#!/usr/bin/perl

use utf8;
use strict;
use warnings;
use English qw( -no_match_vars );
use FindBin qw( $RealBin );
use Test::More;
use Test::Builder;

my $test = Test::Builder->new;

my @programs = glob $RealBin .'/../../*.pl';

if ( @programs ) {
    plan( tests => scalar @programs );

    foreach my $each_path_to_script ( @programs ) {
        pl_file_ok(
            $each_path_to_script,
            $each_path_to_script .' script compiles'
        );
    }
}
else {
    plan( skip_all => 'no scripts are selected' );
}

sub pl_file_ok {
    my ( $path_to_file, $test_name ) = @ARG;

    unless ( -f $path_to_file ) {
        $test->ok( 0, $test_name );
        $test->diag( $path_to_file .' does not exist' );

        return;
    }

    unless ( -x $path_to_file ) {
        $test->ok( 0, $test_name );
        $test->diag( 'permission denied for '. $path_to_file );

        return;
    }

    my $child_output = `$EXECUTABLE_NAME -cw $path_to_file 2>&1`;

    if ( $CHILD_ERROR ) {
        $test->ok( 0, 'script does not compile' );
        $test->diag( $child_output );

        return;
    }
    else {
        $test->ok( 1, $test_name );

        return 1;
    }
}
